package com.kaoker.system.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.ObjectUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.kaoker.system.GlobalInfo;
import com.kaoker.system.dao.IExamPaperDao;
import com.kaoker.system.dao.ISubjectDao;
import com.kaoker.system.entity.ExamPaper;
import com.kaoker.system.entity.Subject;

/**
 * Exam Paper DAO
 * 
 * @author bladestone
 * 
 */
public class ExamPaperDao extends HibernateDaoSupport implements IExamPaperDao {
	@Override
	public void addPaper(ExamPaper paper) {
		if (ObjectUtils.equals(paper, null))
			return;

		this.getHibernateTemplate().save(paper);
	}

	@Override
	public void updatePaper(ExamPaper paper) {
		if (ObjectUtils.equals(paper, null))
			return;

		this.getHibernateTemplate().update(paper);
	}

	@Override
	public List<ExamPaper> getPapersBySubject(Subject subject) {
		if (ObjectUtils.equals(subject, null))
			return new ArrayList<ExamPaper>();

		return this.getPapersBySubject(subject.getId());
	}

	@Override
	public List<ExamPaper> getPapersBySubject(Long id) {
		String queryStryById = "from ExamPaper where SUBJECT_ID=" + id;
		return this.getHibernateTemplate().find(queryStryById);
	}

	@Override
	public List<ExamPaper> getPapersBySubject(String subjectName) {
		ISubjectDao subjectDao = (ISubjectDao) GlobalInfo.getContext().getBean(
				"subjectDAO");
		Subject subject = subjectDao.getSubject(subjectName);
		return getPapersBySubject(subject.getId());
	}

	@Override
	public List<ExamPaper> getPapersBySubject(String subject, Date startDate,
			Date endDate) {
		String queryStrByDate = "from ExamPaper ep where ep.DATE > ? && ep.Date < ?";

		return this.getHibernateTemplate().find(queryStrByDate,
				new Object[] { startDate, endDate });
	}

	@Override
	public void deletePaper(ExamPaper paper) {
		this.getHibernateTemplate().delete(paper);
	}

}
